package com.iyoudoock.citymine.activity.user;

import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.iyoudoock.citymine.widget.RegionPop;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;
import com.kvin.toolkit.utils.region.ProvinceBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditUserInfoActivity extends BaseActivity {

    @BindView(R.id.nick_name_et)
    EditText nickNameEt;
    @BindView(R.id.male_rad)
    RadioButton maleRad;
    @BindView(R.id.female_rad)
    RadioButton femaleRad;
    @BindView(R.id.sex_rg)
    RadioGroup sexRg;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.age_et)
    EditText ageEt;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.detail_address_tv)
    EditText detailAddressEt;
    @BindView(R.id.store_btn)
    Button storeBtn;

    private String nickName;
    private String sex;
    private String phoneNo;
    private String age;
    private String address;
    private String detailAddress;

    private RegionPop mPop;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);
        ButterKnife.bind(this);
    }


    @Override
    public void initNav() {
        setTitle("个人资料");
    }


    @Override
    public void init() {
        initPop();
        initRegion();
        mJsonUtils = JsonUtils.getInstance();
        storeBtn.setOnClickListener(this);
        addressTv.setOnClickListener(this);

        nickName = mPersistentUtils.readString(Key.User.NICK_NAME);
        sex = mPersistentUtils.readString(Key.User.SEX);
        phoneNo = mPersistentUtils.readString(Key.User.TELEPHONE);
        age = mPersistentUtils.readString(Key.User.AGE);
        address = mPersistentUtils.readString(Key.User.ADDRESS);
        detailAddress = mPersistentUtils.readString(Key.User.DISTRICT);

        this.nickNameEt.setText(nickName);
        this.phoneEt.setText(phoneNo);
        this.ageEt.setText(age);
        this.addressTv.setText(address);
        this.detailAddressEt.setText(detailAddress);

        if (sex.equals("男")) {
            ((RadioButton) sexRg.getChildAt(0)).setChecked(true);
        } else if (sex.equals("女")) {
            ((RadioButton) sexRg.getChildAt(1)).setChecked(true);
        }
    }


    /**
     * init region choose pop
     */
    private void initPop() {
        mPop = RegionPop.getInstance(this);
        mPop.setOnCancelClickListener(this);
        mPop.setOnSubmitClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.store_btn:
                updateInfo();
                break;
            case R.id.address_tv:
                mPop.show(addressTv, Gravity.BOTTOM);
                break;
            case R.id.cancel_tv:
                mPop.dismiss();
                break;
            case R.id.confirm_tv:
                setAddress();
                break;
        }
    }


    @SuppressWarnings("unchecked")
    @Override
    public void onHandleMessage(Message msg) {
        switch (msg.what) {
            case Value.Message.REGION:
                mPop.setRegion((List<ProvinceBean>) msg.obj);
                break;
        }
    }


    /**
     * set address
     */
    private void setAddress() {
        String address = mPop.getAddress();
        this.addressTv.setText(address);
        mPop.dismiss();
    }

    /**
     * update user info
     */
    private void updateInfo() {
        nickName = nickNameEt.getText().toString();
        sex = ((RadioButton) findViewById(sexRg.getCheckedRadioButtonId())).getText().toString();
        age = ageEt.getText().toString();
        address = addressTv.getText().toString();
        detailAddress = detailAddressEt.getText().toString();
        HashMap<String, String> params = HttpUtils.updateUserInfo(mUid, nickName, sex, age, address + detailAddress, mLatitude, mLongitude,
                "", "", "", "", "", "", "", "", "", "", "");
        mJsonUtils.post(HttpUtils.UPDATE_USERINFO_URL, params, this, Value.Flag.UPDATE_USER_INFO);
    }


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            if (flag.equals(Value.Flag.UPDATE_USER_INFO)) {
                if (response.getInt(Key.Tag.STATE) == Value.Response.SUCCESS) {
                    ToastUtils.simpleToast(getActivity(), "更新成功！");
                    mPersistentUtils.writeString(Key.User.NICK_NAME, nickName);
                    mPersistentUtils.writeString(Key.User.SEX, sex);
                    mPersistentUtils.writeString(Key.User.AGE, age);
                    mPersistentUtils.writeString(Key.User.ADDRESS, address);
                    mPersistentUtils.writeString(Key.User.ADDRESS, detailAddress);
                    finish();
                } else {
                    ToastUtils.simpleToast(getActivity(), "更新失败！");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }
}
